package com.androidcourse.shounews.MyDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.androidcourse.shounews.R;


public class MyDialog extends Dialog implements View.OnClickListener {

    private TextView  mMessage, mConfirm;
    private String sTitle, sMessage, sConfirm, sCancel;
    private View.OnClickListener cancelListener, confirmListener;


    public MyDialog setsMessage(String sMessage) {
        this.sMessage = sMessage;
        return this;
    }

    public MyDialog setsConfirm(String sConfirm, View.OnClickListener listener) {
        this.sConfirm = sConfirm;
        this.confirmListener = listener;
        return this;
    }

    public MyDialog(@NonNull Context context) {
        super(context);
    }

    public MyDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
//        setCancelable(true);
        //自定义Dialog宽度
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        d.getSize(size);
        p.width = (int) ((size.x)*0.7);        //设置为屏幕的0.7倍宽度
        getWindow().setAttributes(p);
        mMessage = findViewById(R.id.message);
        mConfirm = findViewById(R.id.confirm);
        if (!TextUtils.isEmpty(sMessage)) {
            mMessage.setText(sMessage);
        }
        if (!TextUtils.isEmpty(sConfirm)) {
            mConfirm.setText(sConfirm);
        }
        mConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        long id= view.getId();
        if(id==R.id.confirm) {
            if (confirmListener != null) {
                confirmListener.onClick(view);
            }
        }
        }
    }

